/**
 * 记录上下文信息
 * Created by relevlin on 2015-12-06.
 */

define(['require', "jquery", "public/utils", 'public/dataReady'], function (require, $, Utils, DataReady) {
    var CONST_KEY_TOKEN = "token";
    var CONST_KEY_STU_TOKEN = "stu_token"; //机顶盒设备TOKEN
    var CONST_KEY_USER_PRINCIPAL = "userPrincipal";

    var _basePath = "";//项目的上下文根，页面初始化时，根据${pageContext.request.contextPath}确定
    var utils = new Utils();//utils类的实例
    var _templdateMap={}; //用于全局缓存模板样式
    var _deviceImagePath = "/images/app/topo/";

    /**
     * 构建url地址
     * @param uri
     * @returns {*}
     */
    function urlBuild(uri) {
        if (utils.isBlank(uri)) {
            throw "构建rest的url地址时，uri为空";
        }

        if (uri.substr(0, 1) === "/") {
            return getBasePath() + uri;
        } else {
            return getBasePath() + "/" + uri;
        }
    }

    function getBasePath() {
        return _basePath;
    }

    function setBasePath(basePath) {
        _basePath = basePath;
    }

    function getDeviceImgPath() {
        return getBasePath() + _deviceImagePath;
    }

    function setTempldateMap(templdateMap) {
        _templdateMap = templdateMap;
    }

    function getTempldateMap() {
        return _templdateMap;
    }

    function getTempldate(key) {
        for(var i = 0; i < _templdateMap.length; i ++){
            var template = _templdateMap[i];
            if(template.key ===key){
                return template;
            }
        }
    }

    function buildPage(key,$container) {
        var template = getTempldate(key);
        var Page = require(template.jsPath);
        var page = new Page($container); //如果不需要，可以直接构造页面渲染对象进行模板加载和数据填充
        return page;
    }


    function setUserPrincipal(userPrincipal) {
        console.log('set userPrincipal ', userPrincipal);
        console.log('set userPrincipal.token ', userPrincipal[CONST_KEY_TOKEN]);
        localStorage.setItem(CONST_KEY_USER_PRINCIPAL, JSON.stringify(userPrincipal));
        $.cookie(CONST_KEY_TOKEN, userPrincipal[CONST_KEY_TOKEN]);
    }



    function getUserPrincipal() {
        var jsonStr = localStorage.getItem(CONST_KEY_USER_PRINCIPAL);
        return JSON.parse(jsonStr);
    }

    function getToken() {
        if (!utils.isNone(getUserPrincipal())) {
            return getUserPrincipal()[CONST_KEY_TOKEN];
        } else {
            return null;
        }
    };

    //机顶盒Token
    function setSTU_Token(stu_Token) {
        localStorage.setItem(CONST_KEY_STU_TOKEN, JSON.stringify(stu_Token));
    };

    function getSTU_Token() {
        var stu_Token = localStorage.getItem(CONST_KEY_STU_TOKEN);
        if (!utils.isNone(stu_Token)) {
            return stu_Token;
        } else {
            return "test001";//写死的测试数据
        }

    };

    /**
     * 获取一个DataReady对象
     * @returns {DataReady}
     */
    function getDataReady() {
        return new DataReady();
    };

    /**
     * 在Home页面的home-tabs上打开一个tab
     * @param opts ={
     *   name : tab页的标题
     *   closable : tab页是否允许关闭
     *   href : ajax加载tab页的地址
     *   executeClass : 与ajax加载的tab页对应的页面js对象
     * }
     */
    function openHomeTab(opts) {
        if ($('home-tabs')) {
            var $tab = $("#home-tabs").tabs('getTab', opts.name);
            if (!$tab) {
                if (opts.loadtype === 2) {
                    $('#home-tabs').tabs('add', {
                        title: opts.name,
                        content: '<iframe scrolling="auto" frameborder="0" src="' + opts.href + '"></iframe>',
                        id: opts.id,
                        closable: ('closable' in opts) ? opts['closable'] : true
                    });
                    $("div[id='" + opts.id + "']").css({"overflow": "hidden"});
                } else {
                    $("#home-tabs").tabs("add", {
                        title: opts.name,
                        closable: ('closable' in opts) ? opts['closable'] : true,
                        id: opts.id,
                        href: opts.href,
                        onLoad: function () {
                            var $container = $("#home-tabs").tabs('getSelected');
                            var module = new opts.executeClass($container);
                            $("#home-tabs").tabs("options").opens.push({
                                title: opts.name,
                                index: $("#home-tabs").tabs('getTabIndex', $("#home-tabs").tabs('getTab', opts.name)),
                                module: module
                            })
                        }
                    });
                }
            } else {
                $("#home-tabs").tabs('select', opts.name);
            }
        }
    };

    /**
     * 图片预览
     * @param fileObj
     * @param imgPreviewId
     * @param divPreviewId
     * @constructor
     */
    function previewImage(fileObj,imgPreviewId,divPreviewId){
    };

    function showGraph(){

    }
    return {
        getBasePath: getBasePath,
        setBasePath: setBasePath,
        urlBuild: urlBuild,
        setUserPrincipal: setUserPrincipal,
        getUserPrincipal: getUserPrincipal,
        getToken: getToken,
        utils: utils,
        getDataReady: getDataReady,
        openHomeTab: openHomeTab,
        getSTU_Token:getSTU_Token,
        setSTU_Token:setSTU_Token,
        setTempldateMap:setTempldateMap,
        getTempldateMap:getTempldateMap,
        getTempldate:getTempldate,
        buildPage:buildPage,
        previewImage:previewImage,
        getDeviceImgPath:getDeviceImgPath
    }
});